/****************
 *@description:  批量发货  @/orderManage/bulkShipping
 *@author: Xhd
 *@API: 
 *@date: 2023-11-22 11:13:57
 *@version: V1.0.0
*************************************************************************/

import React, { useState, useEffect, useRef } from "react";
import { Table, Button, Upload, message } from "antd";
import TheQueryForm from '@/components/TheQueryForm';
import TheButton from '@/components/TheButton';
import { apiBatchSendPage, apiStoreSendOrderBatchSendOrder } from "@/api/orderManage/index.js";

const BulkShipping = () => {
  const queryFormRef = useRef()
  const [searchOption, setSearchOption] = useState([
    { field: 'storeSendOrderTaskId', label: '任务id', placeholder: '请输入任务id', required: false, rules: [], type: 'Input', defaultValue: '' },
    { field: 'time', label: '操作时间', placeholder: '请选择', required: false, rules: [], type: 'RangePicker' },
    { field: 'state', label: '状态', placeholder: '请选择', required: false, rules: [], type: 'Select', options: [{ label: '全部', value: '', }, { label: '进行中', value: 0 }, { label: '已结束', value: 1 }], defaultValue: '' },
    {}
  ])
  const [queryForm, setQueryForm] = useState({})
  const [columns, setColumns] = useState([
    {
      title: '任务id',
      dataIndex: 'storeSendOrderTaskId',
      key: 'storeSendOrderTaskId',
    },
    {
      title: '发货成功单数',
      dataIndex: 'successCount',
      key: 'successCount',
    },
    {
      title: '发货失败单数',
      dataIndex: 'failCount',
      key: 'failCount',
    },
    {
      title: '状态',
      dataIndex: 'state',
      key: 'state',
      render: (text) => {
        return text === 0 ? "进行中" : "已结束"
      }
    },
    {
      title: '操作时间',
      dataIndex: 'createTime',
      key: 'createTime',
    },
    {
      title: '操作',
      key: 'operate',
      render(text, record) {
        return record.remark !== "-" ? <TheButton type="link" haspermi={['bulkShipping:batch:logs']} ><a href={record.fileUrl}>{record.remark}</a></TheButton> : "-"
      }
    },
  ])
  const [dataSource, setDataSource] = useState([])
  const [pageData, setPageData] = useState({ pageNo: 1, pageSize: 10 })
  const [total, setTotal] = useState(0)
  //批量发货-导入
  const uploadProps = {
    name: 'file',
    maxCount: 1,
    showUploadList: false,
    onChange: (info) => {
      if (info.file.status === 'done') {
        message.success('上传成功');
      }
      if (info.file.status === 'error') {
        message.error('上传失败');
      }
    },
    customRequest: (option) => {
      let params = {
        file: option.file,
      }
      apiStoreSendOrderBatchSendOrder(params).then(res => {
        if(res.data) {
          message.success('上传成功');
          getData({ pageNo: 1, pageSize: 10 })
        }
      })
    }
  }

  useEffect(() => {
    getData(pageData)
  }, [])

  //获取数据
  let getData = async (values) => {
    await apiBatchSendPage({ ...values }).then(res => {
      let data = res.data.map((item, index)=>{
        item.key = index
        return item
      })
      setDataSource(data)
      setTotal(res.totalCount)
    })
  }
  //搜索
  let onFormSearch = (values) => {
    if (values && values.hasOwnProperty('time')) {
      values.createTimeStart = values.time[0].format('YYYY-MM-DD') + ' 00:00:00'
      values.createTimeEnd = values.time[1].format('YYYY-MM-DD') + ' 23:59:59'
      delete values.time
    }
    setQueryForm(values)
    getData({ ...values, pageNo: 1, pageSize: 10 })
  }
  //重置
  let onReset = () => {
    setQueryForm({})
    getData({ pageNo: 1, pageSize: 10 })
  }
  //翻页
  let pageChange = (pageNo, pageSize) => {
    setPageData({ pageNo, pageSize })
    getData({ ...queryForm, pageNo, pageSize })
  }

  return (
    <div className='wrap-box wrap-box-content'>
      <TheQueryForm ref={queryFormRef} show={true} option={searchOption}
        onGetValues={(values) => onFormSearch(values)}
        onReset={(values) => onReset(values)}
      >
        <Upload {...uploadProps}><TheButton haspermi={['bulkShipping:batch:shipments']} size="middle" type="primary">批量发货</TheButton></Upload>
        <TheButton size="middle" type="link" haspermi={['bulkShipping:batch:demo']}><a href="http://hqmimage.oss-cn-beijing.aliyuncs.com/模板1701079279138.xlsx">下载批量发货模板</a></TheButton>
      </TheQueryForm>
      <Table
        bordered
        columns={columns}
        dataSource={dataSource}
        pagination={{
          position: ['bottomCenter'],
          current: pageData.pageNo,
          pageSize: pageData.pageSize,
          total: total,
          onChange: pageChange,
          showSizeChanger: false
        }}
      ></Table>
    </div>
  )
}

export default BulkShipping
